<?php
class UserParameters
{
  var $vars;
  var $errors;
  var $s;

  function UserParameters($table = "gparams", $userrow = "user", $namerow = "name", $subnavalrow = "subnaval", $valuerow = "value", $errorhandler = "glog") {
    global $pfx;
    
    $table = $pfx.$table;
    
    $this->vars = array();
    $this->errors = array();
    $this->s = array('t'=>$table, 'u'=>$userrow, 'n'=>$namerow, 'v'=>$valuerow, 's'=>$subnavalrow, 'e'=>$errorhandler);

    $res = @mysql_query("SELECT * FROM ".$table." WHERE ".$userrow."='' OR ".$userrow."='".$_SESSION['user']."'");
    if(!$res) {
      $this->addError("Constructor: MySQL-Query failed: ".@mysql_errno().": ".mysql_error());
      return false;
    } else {
      while($row = @mysql_fetch_assoc($res)) {
        if(empty($row[$subnavalrow])) {
          if(!(isset($this->vars[$row[$namerow]]) && $row[$userrow]=="")) {
            $this->vars[$row[$namerow]] = array('def'=>"", 'accesslevel'=>0, 'callback'=>"", 'mysql'=>true, 'value'=>$row[$valuerow], 'subs'=>array());
          }
        } else {
          if(!(isset($this->vars[$row[$namerow]]['subs'][$row[$subnavalrow]]) && $row[$userrow]=="")) {
            if(!isset($this->vars[$row[$namerow]])) {
              $this->vars[$row[$namerow]] = array('def'=>"", 'accesslevel'=>0, 'callback'=>"", 'mysql'=>false, 'value'=>"", 'subs'=>array());
            }
            $this->vars[$row[$namerow]]['subs'][$row[$subnavalrow]] = array('def'=>"", 'accesslevel'=>0, 'callback'=>"", 'mysql'=>true, 'value'=>$row[$valuerow]);
          }
        }
      }
      return true;
    }
  }
  
  function addError($error)
  {
    $e = $this->s['e'];
    global $$e;
    
    $$e->add("gparams", $error);
  }
  
  function def($name, $value, $subnaval="", $val_callback = "", $accesslevel = 1)
  {
    $error = " - name: ".$name."; subnaval: ".$subnaval."; value: ".$value."; callback: ".$val_callback."; accesslevel: ".$accesslevel;
    if(empty($name)) {
      $this->addError("def-Function: Name is empty".$error);
      return false;
    }
    if(is_array($value)) {
      $this->addError("def-Function: Value is array".$error);
      return false;
    }
    if(!is_numeric($accesslevel)) {
      $this->addError("def-Function: Accesslevel isn't numeric".$error);
      return false;
    }
    if(!empty($val_callback) && !is_callable($val_callback)) {
      $this->addError("def-Function: Callback-Function isn't callable".$error);
      return false;
    }
    if(!isset($this->vars[$name])) {
      $this->vars[$name] = array('value'=>"", 'mysql'=>false);
    }
    if(empty($subnaval)) {
      $this->vars[$name]['def'] = $value;
      $this->vars[$name]['accesslevel'] = $accesslevel;
      $this->vars[$name]['callback'] = $val_callback;
      if(!empty($val_callback)) {
        $this->vars[$name]['value'] = call_user_func($val_callback, $this->vars[$name]['value']);
      }
    } else {
      $this->vars[$name]['subs'][$subnaval]['def'] = $value;
      $this->vars[$name]['subs'][$subnaval]['accesslevel'] = $accesslevel;
      $this->vars[$name]['subs'][$subnaval]['callback'] = $val_callback;
      if(!empty($val_callback)) {
        $this->vars[$name]['subs'][$subnaval]['value'] = call_user_func($val_callback, $this->vars[$name]['subs'][$subnaval]['value']);
      }
    }
    return true;
  }
  
  function set($name, $value, $subnaval="", $user = "", $overwrite = false) {
    if(empty($user)) {
      $user = $_SESSION['user'];
    } else if($user == "-no-") {
      $user = "";
    }
    $error = " - name: ".$name."; value: ".$value."; subnaval: ".$subnaval."; user: ".$user;
    if(empty($name)) {
      $this->addError("set-Function: Name is empty".$error);
      return false;
    }
    if(!isset($this->vars[$name])) {
      $this->addError("set-Function: Name isn't defined".$error);
      return false;
    }
    
    $exec_my = true;
    if(empty($subnaval)) {
      if($value == $this->vars[$name]['def']) {
        $this->vars[$name]['value'] = $value;
        $exec_my = false;
      }
    } else {
      if($value == $this->vars[$name]['subs'][$subnaval]['def']) {
        $this->vars[$name]['subs'][$subnaval]['value'] = $value;
        $exec_my = false;
      }
    }
    if($overwrite) {
      $exec_my = true;
    }
    if($exec_my) {
      if((!$this->vars[$name]['mysql'] && empty($subnaval)) || (!empty($subnaval) && !$this->vars[$name]['subs'][$subnaval]['mysql'])) {
        $sql = "INSERT INTO `".$this->s['t']."` (`".$this->s['n']."`, `".$this->s['v']."`, `".$this->s['u']."`, `".$this->s['s']."`) VALUES ('".$name."', '".$value."', '".$user."', '".$subnaval."')";
      } else {
        $sql = "UPDATE `".$this->s['t']."` SET `".$this->s['v']."` = '".$value."' WHERE `".$this->s['n']."` = '".$name."' AND `".$this->s['s']."` = '".$subnaval."' AND `".$this->s['u']."` = '".$user."'";
      }
      if(@mysql_query($sql)) {
        if(empty($subnaval)) {
          $this->vars[$name]['value'] = $value;
          if(!empty($this->vars[$name]['callback'])) {
            $this->vars[$name]['value'] = call_user_func($this->vars[$name]['callback'], $this->vars[$name]['value']);
          }
        } else {
          $this->vars[$name]['subs'][$subnaval]['value'] = $value;
          if(!empty($this->vars[$name]['subs'][$subnaval]['callback'])) {
            $this->vars[$name]['subs'][$subnaval]['value'] = call_user_func($this->vars[$name]['subs'][$subnaval]['callback'], $this->vars[$name]['subs'][$subnaval]['value']);
          }
        }
        return true;
      } else {
        $this->addError("set-Function: MySQL-Query failed: ".@mysql_errno().": ".mysql_error());
        return false;
      }
    }
  }
  
  function get($name, $subnaval = "")
  {
    $error = " - name: ".$name."; subnaval: ".$subnaval;
    if(empty($name)) {
      $this->addError("get-Function: Name is empty".$error);
      return false;
    }
    if(!isset($this->vars[$name])) {
      $this->addError("get-Function: Name isn't defined".$error);
      return false;
    }
    
    if(empty($subnaval)) {
      $value = $this->vars[$name]['value'];
      $def = $this->vars[$name]['def'];
    } else {
      $value = $this->vars[$name]['subs'][$subnaval]['value'];
      $def = $this->vars[$name]['subs'][$subnaval]['def'];
    }
    
    if(!empty($value)) {
      return $value;
    } else {
      return $def;
    }
  }
  
  function compare($name, $subnaval = "")
  {
    $error = " - name: ".$name."; subnaval: ".$subnaval;
    if(empty($name)) {
      $this->addError("compare-Function: Name is empty".$error);
      return false;
    }
    if(!isset($this->vars[$name])) {
      $this->addError("compare-Function: Name isn't defined".$error);
      return false;
    }
    
    if(empty($subnaval)) {
      $value = $this->vars[$name]['value'];
      $def = $this->vars[$name]['def'];
    } else {
      $value = $this->vars[$name]['subs'][$subnaval]['value'];
      $def = $this->vars[$name]['subs'][$subnaval]['def'];
    }
    
    if($value==$def) {
      return true;
    }
    return false;
  }
  
  function getSubs($name)
  {
    $error = " - name: ".$name;
    if(empty($name)) {
      $this->addError("getSubs-Function: Name is empty".$error);
      return false;
    }
    if(!isset($this->vars[$name])) {
      $this->addError("getSubs-Function: Name isn't defined".$error);
      return false;
    }
    
    $a = array();
    foreach($this->vars[$name]['subs'] as $p=>$q) {
      $a[] = $p;
    }
    return $a;
  }
  
  function getAccesslevel($name, $subnaval = "")
  {
    $error = " - name: ".$name."; subnaval: ".$subnaval;
    if(empty($name)) {
      $this->addError("getAccesslevel-Function: Name is empty".$error);
      return false;
    }
    if(!isset($this->vars[$name])) {
      $this->addError("getAccesslevel-Function: Name isn't defined".$error);
      return false;
    }
    if(empty($subnaval)) {
      return $this->vars[$name]['accesslevel'];
    } else {
      return $this->vars[$name]['subs'][$subnaval]['accesslevel'];
    }
  }
}

$params = new UserParameters("gparams", "user", "name", "subnaval", "value", "glog");
?>